package com.zhima.dto;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhima.exception.ErrorCode;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
public class ZhimaOjPageResult extends BaseResultDTO {
    /**
     * 本页数据
     */
    private List rows;
    /**
     * 记录总数
     */
    private long totalSum;
    /**
     * 当前页号
     */
    private long pageNum;


    public ZhimaOjPageResult(String errorMessage, boolean success, List rows, long totalSum, long pageNum) {
        super(errorMessage, success);
        this.rows = rows;
        this.totalSum = totalSum;
        this.pageNum = pageNum;
    }

    public static ZhimaOjPageResult success(List rows, long totalSum, long pageNum) {
        return new ZhimaOjPageResult(null, true, rows, totalSum, pageNum);
    }

    public static ZhimaOjPageResult fail() {
        return new ZhimaOjPageResult(null, false, null, -1, -1);
    }

    public static ZhimaOjPageResult fail(String errorMessage) {
        return new ZhimaOjPageResult(errorMessage, false, null, -1, -1);
    }

    public static ZhimaOjPageResult fail(ErrorCode errorCode) {
        return new ZhimaOjPageResult(errorCode.getErrorMessage(), false, null, -1, -1);
    }

    public static ZhimaOjPageResult success(Page page) {
        return success(
                page.getRecords(),
                page.getTotal(),
                page.getCurrent()
        );
    }
}